<template>
  <div class="app-container over-scroll">
    <el-row :gutter="20" class="mb20">
      <el-col :span="4">
        <!-- <SelectWarnName v-model="req.ruleCode" @change="getList"></SelectWarnName> -->
      </el-col>
      <el-col :span="4"><el-input v-model="req.content" type="text" placeholder="请输入告警内容" @change="getList"></el-input></el-col>
      <el-col :span="4"><el-input v-model="req.deviceName" type="text" placeholder="请输入设备地址" @change="getList"></el-input></el-col>
      <el-col :span="4">
        <SelectDateRange :start.sync="req.startTime" :end.sync="req.endTime" @change="getList"></SelectDateRange>
      </el-col>
      <el-col :span="4">
        <el-select style="width: 100%" v-model="req.status" placeholder="请选择告警状态" clearable @change="getList">
          <el-option label="告警中" :value="1"></el-option>
          <el-option label="已忽略" :value="-1"></el-option>
          <el-option label="已恢复" :value="0"></el-option>
        </el-select>
      </el-col>
      <el-col :span="2">
        <el-button type="primary" icon="el-icon-search" @click="getList">查询</el-button>
        <!-- <el-button type="warning" @click="down">导出</el-button> -->
      </el-col>
    </el-row>
    <el-table :data="list" border style="width: 100%">
      <el-table-column type="index" label="序号" width="60" align="center" show-overflow-tooltip>
        <template slot-scope="{$index}">
          {{$index + 1 + (req.page - 1) * req.pageSize}}
        </template>
      </el-table-column>
      <el-table-column prop="ruleName" label="预警类型" min-width="120" align="left" show-overflow-tooltip></el-table-column>
      <!-- <el-table-column prop="deviceName" label="设备名称" min-width="120" align="left" show-overflow-tooltip></el-table-column> -->
      <el-table-column prop="deviceName" label="设备地址" min-width="120" align="left" show-overflow-tooltip></el-table-column>
      <el-table-column prop="content" label="告警内容" min-width="120" align="left" show-overflow-tooltip></el-table-column>
      <el-table-column prop="gmtCreate" label="告警时间" min-width="120" align="left" show-overflow-tooltip></el-table-column>
      <el-table-column prop="gmtLast" label="最近上报时间" min-width="120" align="left" show-overflow-tooltip></el-table-column>
      <el-table-column prop="inc" label="持续次数" min-width="120" align="left" show-overflow-tooltip></el-table-column>
      <el-table-column prop="status" label="状态" min-width="120" align="left" show-overflow-tooltip>
        <template slot-scope="{ row }">
          {{ row.eventId ? '已转事件':(row.status == 1 ? '告警中' : row.status == -1 ? '已忽略' : row.status == 0 ? '已恢复' : '其他') }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="150" align="center">
        <template slot-scope="{ row }">
          <el-button @click="see(row)" type="text" size="small">详情</el-button>
          <el-button v-if="row.status==1" @click="ignore(row)" type="text" size="small">忽略</el-button>
          <!-- <el-button v-if="!row.eventId && row.status==1" @click="toEvent(row)" type="text" size="small">转事件</el-button> -->
        </template>
      </el-table-column>
    </el-table>
    <Pagination v-show="total" :total="total" :page.sync="req.page" :limit.sync="req.pageSize" @pagination="getList"></Pagination>

    <!-- 弹窗 -->
    <dtl :show.sync="show" :item="current" @showEvent="onShowEvent"></dtl>
    <convertToEvent :show.sync="showToEvent" :item="current" @change="getList"></convertToEvent>

    <eventDtl :show.sync="showEvent" :item="currentEvent"></eventDtl>
  </div>
</template>

<script>
import dtl from './components/dtl.vue';
import eventDtl from '@/views/eventManage/components/detail.vue'
import convertToEvent from './components/convertToEvent.vue';
import { alarmList, alarmIgnore, getEventKey } from '@/api/alarm';


export default {
  name: 'Card',
  components: {
    dtl,
    convertToEvent,
    eventDtl
  },
  data() {
    return {
      show: false,
      showToEvent: false,
      current: null,
      list: [],
      total: 0,
      req: {
        startTime: null,
        endTime: null,
        status: 1,
        page: 1,
        pageSize: 20,
      },
      currentEvent: null,
      showEvent: false
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 获取列表
    getList() {
      alarmList(this.req).then((res) => {
        this.list = res.data || [];
        this.total = res.total;
      });
    },
    // 忽略
    ignore(row) {
      this.$modal.confirm('确定忽略此告警吗？').then((res) => {
        // 请求服务器删除数据
        alarmIgnore({ id: row.id }).then((res) => {
          // 刷新数据
          this.getList();
        });
      });
    },
    // 查看
    see(row) {
      this.current = JSON.parse(JSON.stringify(row));
      this.show = true;
    },
    // 转事件
    toEvent(row) {
      const location = row.location ? (row.location.trim().startsWith('[')) ? row.location: `[${row.location}]`: null
      this.current = {
        id: row.id,
        location: location,
        content: row.content,
        address: row.address||row.deviceName,
        pics: row.image
      };
      this.showToEvent = true;
    },
    // 显示事件详情
    async onShowEvent(item) {
      try {
        const res = await getEventKey({eventInstanceId: item.eventId})
        this.currentEvent = res.data;
        this.showEvent = true;
      } catch (error) {

      }
    },
    down() {
      this.download('/renovation/alarm/export', this.req, 'get')
    }
  }
};
</script>

<style lang="scss" scoped></style>
